package com.joysuch.wwyt.patrol.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.patrol.entity.bean.PatrolDataTypeSearchBean;
import com.joysuch.wwyt.patrol.entity.bean.PatrolOptionSearchBean;
import com.joysuch.wwyt.patrol.entity.dto.PatrolDataTypeDto;
import com.joysuch.wwyt.patrol.entity.dto.PatrolOptionDto;
import com.joysuch.wwyt.patrol.entity.vo.PatrolDataTypeVo;
import com.joysuch.wwyt.patrol.service.PatrolDataTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lrf
 * @since 2022-03-01
 */
@RestController
@RequestMapping("/patrol/patrol-data-type")
@Api(tags = "智能巡检-数据类型管理-状态")
@CrossOrigin
public class PatrolDataTypeController {

    @Autowired
    private PatrolDataTypeService patrolDataTypeService;

    @GetMapping("/getDataType")
    @ApiOperation(value = "获取数据类型列表(状态)")
    public ResultBean getDataType( PatrolDataTypeSearchBean dataTypeSearchBean){
        Page<PatrolDataTypeDto> page = patrolDataTypeService.findDataTypePageList(dataTypeSearchBean);
        return ResultBean.pageData(page.getRecords(),page.getTotal());
    }

    @PostMapping("/saveDataType")
    @ApiOperation(value = "保存数据类型列表")
    @RequiresPermissions("patrol:patrol-data-type:saveDataType")
    public ResultBean saveDataType(@RequestBody PatrolDataTypeVo dataTypeVo) {

        try {
            boolean save = patrolDataTypeService.saveDataType(dataTypeVo);
            return save ? ResultBean.defaultSuccessResult() : ResultBean.fail(Status.FAILED);
        } catch (ApiBusinessException e) {
            e.printStackTrace();
            return ResultBean.fail(e.getCode(),e.getMessage());
            
        }

    }

    @GetMapping("/delectDataType")
    @ApiOperation(value = "删除数据类型")
    @RequiresPermissions("patrol:patrol-data-type:delectDataType")
    public ResultBean delectDataType(@RequestParam Long id){
        try {
            patrolDataTypeService.deleteDataTypeId(id);
            return ResultBean.defaultSuccessResult();
        } catch (ApiBusinessException e) {
            e.printStackTrace();
            return ResultBean.fail(e.getCode(),e.getMessage());
        }
        
    }

    @GetMapping("/getOption")
    @ApiOperation(value = "获取数据类型列表(数值)")
    public ResultBean getOption(PatrolOptionSearchBean dataTypeSearchBean){
        Page<PatrolOptionDto> page = patrolDataTypeService.findOptionPage(dataTypeSearchBean);
        return ResultBean.pageData(page.getRecords(),page.getTotal());

    }


}

